<template>
	<view class="move-path" @touchend="handleOnTouchEnd">
		<view class="move-content">
			<view v-if="vertify" class="success">验证通过</view>
			<view class="move-content-text" v-else style="display: flex;align-items: center;justify-content: center;">
				向右滑动完成抢单
				<view v-if="shortDispachExpire && shortDispachExpire > 0" style="display: flex;align-items: center;justify-content: center;">(<u-count-down :time="shortDispachExpire" format="mm:ss" @change="coutDownChange"></u-count-down>)</view>
			</view>
		</view>
		<movable-area :animation="true">
			<movable-view ref="movableView" class="move-view" :x="x" :y="0" direction="all" @change="handleOnMoving" :disabled="vertify"
				:class="{'active':vertify}"></movable-view>
		</movable-area>
	</view>
</template>

<script>
	export default {
		name: 'move-vertify',
		props:{
			shortDispachExpire:null
		},
		data() {
			return {
				x: 0,
				oldx: 0,
				vertify: false,
				size: {},
				isMove: false
			};
		},
		mounted() {
			this.init();
			uni.$on('restVertify', res => {
				this.x = 0;
				setTimeout(() => {
					this.vertify = false;
				},1200)
			})
		},
		methods: {
			init() {
				this.getSize(".move-path").then((pathway) => {
					this.size.pathway = pathway;
					this.getSize(".move-view").then((track) => {
						this.size.track = track;
					});
				})
			},
			/**
			 * 获取元素宽度
			 */
			getSize(selector) {
				return new Promise((resolve, reject) => {
					const view = uni.createSelectorQuery().in(this).select(selector);
					view.fields({
						size: true,
					}, (res) => {
						resolve(res.width);
					}).exec();
				});
			},
			/**
			 * 滑动过程
			 */
			handleOnMoving(e) {
				this.oldx = e.detail.x;
			},
			/**
			 * 滑动结束
			 */
			handleOnTouchEnd() {
				if (this.vertify || this.oldx < 1) return;
				this.x = this.oldx;
				if ((this.oldx + 50) > (this.size.pathway - this.size.track)) this.vertify = true;
				else {
					this.$nextTick(() => {
						this.x = 0;
						this.oldx = 0;
					});
				}
				this.$emit("vertify", this.vertify);
			},
			coutDownChange(e){
				let data = e;
				if(e.milliseconds == 0){
					this.$emit("coutDownChangeTwo", data);
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.move-path {
		color: #333;
		height: 86rpx;
		width: 100%;
		border-radius: 24rpx;
		background: linear-gradient(to right, #418AFF, #418AFF);
		position: relative;
		overflow: hidden;

		.move-content {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
			font-size: 32rpx;
			z-index: 3;

			.success {
				color: #fff;
			}
			.move-content-text{
				/deep/ .u-count-down{
					margin-left: 5rpx;
					text{
						font-size: 32rpx;
						color: #fff;
					}
				}
			}
		}

		movable-area {
			position: absolute;
			top: 0;
			left: 0;
			height: 100%;
			width: 100%;
			background: none;
			z-index: 5;
		}

		movable-view {
			width: 140rpx;
			height: 100%;
			box-sizing: border-box;
			background-position: left;
			background-repeat: no-repeat;
			background-size: auto 86rpx;
			background-image: url('./icon01.png');
		}

		movable-view.active {
			background-position: right;
			background-size: auto 86rpx;
			background-image: url('./icon01.png');
		}
	}
</style>
